﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DocumentFunctions.cs" company="Arizona State University">
//   Copyright (C) 2011-2012 Sukru Tikves and Arizona State University
//   
//   Released under Microsoft Public License (Ms-PL)
//   See http://cipsdata.codeplex.com/license for up to date license 
//   informaiton.
// </copyright>
// <summary>
//   The document functions.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Cips.DataMining
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// The document functions.
    /// </summary>
    public static class DocumentFunctions
    {
        /// <summary>
        /// Get the transitive pairs in a document. This function can be used to build a
        /// Markov network of keyword transition probabilities.
        /// </summary>
        /// <typeparam name="T">Keyword type.</typeparam>
        /// <param name="document">The document.</param>
        /// <returns>A stream of transitive pairs.</returns>
        public static IEnumerable<Tuple<T, T>> TransitivePairs<T>(IEnumerable<T> document)
        {
            var first = true;
            var prev = default(T);

            foreach (var item in document)
            {
                if (first)
                    first = false;
                else
                    yield return new Tuple<T, T>(prev, item);

                prev = item;
            }
        }

        /// <summary>
        /// The top-k vector filter.
        /// </summary>
        /// <param name="vector">
        /// The input vector.
        /// </param>
        /// <param name="k">
        /// Requested k.
        /// </param>
        /// <typeparam name="T">
        /// Keyword type.
        /// </typeparam>
        /// <returns>
        /// The <see cref="SparseTermVector{T}"/> instance with the top-k items from the input.
        /// </returns>
        public static SparseTermVector<T> TopKFilter<T>(SparseTermVector<T> vector, int k)
        {
            return new SparseTermVector<T>(vector.Dictionary, vector.OrderByDescending(v => v.Value).Take(k));
        }

        /// <summary>
        /// The top-k vector filter.
        /// </summary>
        /// <param name="k">
        /// Requested k.
        /// </param>
        /// <typeparam name="T">
        /// Keyword type.
        /// </typeparam>
        /// <returns>
        /// The filter function.
        /// </returns>
        public static Func<SparseTermVector<T>, SparseTermVector<T>> TopKFilter<T>(int k)
        {
            return v => TopKFilter(v, k);
        }
    }
}
